var BSTIterator = function (root) {
  this.values = [];
  const recur = root => {
    if (!root) return;
    if (root.left) recur(root.left);
    this.values.push(root.val);
    if (root.right) recur(root.right);
  }
  recur(root);
}

BSTIterator.prototype.next = function () {
  return this.values.shift();
}

BSTIterator.prototype.hasNext = function () {
  return this.values.length > 0;
}